package com.dabai.simpleconnection_udp.db;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import com.dabai.simpleconnection_udp.bean.UdpCommandModel;

public class SQLiteDatabaseHelper {

    /**
     * 删除表
     *
     * @param db        数据库
     * @param tableName 表名称
     */
    public static void deleteTable(SQLiteDatabase db, String tableName) {
        String sql = "DROP TABLE IF EXISTS " + tableName;
        db.execSQL(sql);
    }

    /**
     * 插入数据
     *
     * @param db              数据库
     * @param udpCommandModel udp命令模型
     * @return long
     */
    public static long insertData(SQLiteDatabase db, UdpCommandModel udpCommandModel){

        ContentValues contentValues = new ContentValues();
        contentValues.put("name", udpCommandModel.getName());
        contentValues.put("note", udpCommandModel.getNote());
        contentValues.put("ip", udpCommandModel.getIp());
        contentValues.put("port", udpCommandModel.getPort());
        contentValues.put("content", udpCommandModel.getContent());

        return db.insert("UDPSet", null, contentValues);
    }

    /**
     * 按id del
     *
     * @param db 数据库
     * @param id id
     */
    public static void delById(SQLiteDatabase db,long id){
        String selection = "id=?";
        String[] selectionArgs = {String.valueOf(id)};
        int deletedRows = db.delete("UDPSet", selection, selectionArgs);
    }


    /**
     * 按id更新
     *
     * @param db              数据库
     * @param id              id
     * @param udpCommandModel udp命令模型
     * @return int
     */
    public static int updateById(SQLiteDatabase db,long id,UdpCommandModel udpCommandModel){

        String selection = "id=?";
        String[] selectionArgs = {String.valueOf(id)};

        ContentValues contentValues = new ContentValues();
        contentValues.put("name", udpCommandModel.getName());
        contentValues.put("note", udpCommandModel.getNote());
        contentValues.put("ip", udpCommandModel.getIp());
        contentValues.put("port", udpCommandModel.getPort());
        contentValues.put("content", udpCommandModel.getContent());

        return db.update("UDPSet", contentValues, selection, selectionArgs);

    }


}
